
package gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import urbanparks.User;

/**
 * Class that displays the login screen.
 * 
 * @author Michelle Miller
 * @version Winter 2012
 */
@SuppressWarnings("serial")
public class LogInPanel extends JPanel {
 
  /** The label that prompts for the email address. */
  private static final JLabel EMAIL_LABEL =
      new JLabel("Email Address: ");

  /**
   * Textfield that gets the email address from the user.
   */
  private final JTextField my_email_textbox =
      new JTextField(20);

  /** The button that the user presses to log in. */
  private final JButton my_submit_button =
      new JButton("Log-In");

  /** A reference to the controller. */
  private final UrbanParksManagerFrame my_manager;

  /**
   * Constructor for the LogInPanel. This sets up the panel
   * to be displayed.
   * 
   * @param the_manager A reference to the controller.
   */
  public LogInPanel(
      final UrbanParksManagerFrame the_manager) {
    super(new GridBagLayout());
    my_manager = the_manager;
    initButton();
    initTextBox();
    final JPanel tempPanel = new JPanel();
    tempPanel.add(EMAIL_LABEL);
    tempPanel.add(my_email_textbox);
    tempPanel.add(my_submit_button);
    this.add(tempPanel, new GridBagConstraints());    
    
  }
  
  /**
   * Adds an actionListener to the textField to listen for
   * an enter key and performs the login actions.
   */
  private void initTextBox() {
    my_email_textbox.addActionListener(
        new ActionListener() {

          public void actionPerformed(
              final ActionEvent the_e) {
            loginActions();
          }
        });
  }

  /**
   * Adds an actionListener to the login Button to listen
   * for a key press and performs the login actions.
   */
  private void initButton() {
    my_submit_button.setText("LOG-IN");
    my_submit_button.addActionListener(
        new ActionListener() {

          public void actionPerformed(
              final ActionEvent the_e) {
            loginActions();
          }
        });
  }

  /**
   * Private helper method that performs the actions that
   * occurs when the user presses the submit button.
   */
  private void loginActions() {
    final String email = my_email_textbox.getText();
    final User newUser =
        my_manager.getUserSet().getUser(email.trim());
    if (newUser == null) {
      final String errorMsg =
          "<html>The e-mail entered was not recogniz" + 
              "ed.<br><center>Please try again.</center></html>";
      JOptionPane.showMessageDialog(my_manager,
                                    errorMsg);
      my_email_textbox.setText("");
    } else {
      my_manager.setCurrentUser(newUser);
      my_manager.switchToMainPage();
    }
  }

  /** Resets the text in the textfield. */
  public void reset() {
    my_email_textbox.setText("");
  }

}
